package cn.jdy.plugins.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.entity.Research;
import cn.jdy.plugins.service.ResearchService;

/**
 * @author yym
 * @title 调查研究控制类
 * 市局已归为news
 * @date 2016/02/26
 */
@Controller
@RequestMapping("/research")
public class ResearchCtrl extends BaseCtrl {
	@Autowired
	private ResearchService researchService;
	@Autowired
	private ConfService confService;

	
	/**
	 * 查询调查研究列表
	 * @param Research
	 * @return
	 */
	@RequestMapping("/index.html")
	public ModelAndView query(Research research) {
		ModelAndView mv = new ModelAndView("/research/researchList.jsp");
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);
		
		try {
			// 设置分页信息
			if (research.getiDisplayLength() == 0)
				research.setiDisplayLength(24); // 默认长度9
			if (research.getPageNo() <= 0)
				research.setPageNo(1); // 默认页数1
			research.setiDisplayStart((research.getPageNo() - 1)
					* research.getiDisplayLength()); // 显示起始索引
			research.setStatus("1");// 状态 0：无效，1：有效
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			if (research.getKeyword() != null && !research.getKeyword().trim().isEmpty()) {
				mv.addObject("dataList",researchService.queryByLike(research, research.getKeyword()));
				mv.addObject("total", researchService.queryByLikeCount(research, research.getKeyword()));
			} else {
				mv.addObject("dataList", researchService.query(research));
				mv.addObject("total", researchService.count(research));
			}
			mv.addObject("pageNo", research.getPageNo());
			mv.addObject("size", research.getiDisplayLength());
			mv.addObject("research", research);
			mv.addObject("key", research.getKeyword()); 
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return mv;
	}

	/**
	 * 调查研究详细内容页
	 * @param id
	 * @return
	 */
	@RequestMapping("/show-{id}.html")
	public ModelAndView queryDetail(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/research/researchDetail.jsp");
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);
		
		try {
			Research research = researchService.find(id);
			mv.addObject("research", research);
			// 增加一次查看
			if (research != null) {
				research.setReadTimes(research.getReadTimes() == null ? 1 : research
						.getReadTimes() + 1);
				researchService.update(research);
			}
			//特警
			String status = research.getSignStatus();
			String[] zhiduis = null;
			String[] assos = research.getDepars().split(",");
			List<String> realAssos = new ArrayList<String>();
			List<String> notAssos = new ArrayList<String>();
			int len = 0,cnt=0;
			//公安-签收状态有别于特警，直接加入签收用户名，用,逗号分隔
			if(status!=null && status!=""){
				zhiduis = status.split(",");//已签收单位String数组
			}
			int tempIndex=0;
			for(int i=1;i<assos.length;i++){
				if((assos[i]!=null &&!(assos[i].equals(";")))){
 					realAssos.add(cnt, assos[i]);
					//未签收
					if(zhiduis!=null && zhiduis.length>0){
						int tempCnt=0;
						for(int it=0;it<zhiduis.length;it++){
							if(!(assos[i].equals(zhiduis[it]))){
								tempCnt++;
							}
						}
						if(tempCnt==zhiduis.length){
							notAssos.add(tempIndex, assos[i]);
							tempIndex++;
						}
					}else{
						notAssos.add(cnt, assos[i]);
					}
					cnt++;
				}
			}
			mv.addObject("realAssos", realAssos);//需要签收的单位gaj
			mv.addObject("signAssos", zhiduis);//已签收的支队名称 gaj
			mv.addObject("notAssos", notAssos);//未签收的支队名称 gaj
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "通知通报详细内容页异常", e);
		}
		return mv;
	}

	/**
	 * 首页初始化
	 * @param Research
	 * @return
	 */
	@RequestMapping("/ind")
	@ResponseBody
	public Map<String, Object> queryN(Research research) {
		Map<String, Object> resp = new HashMap<>();
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		try {
			// 设置分页信息
			if (research.getiDisplayLength() == 0)
				research.setiDisplayLength(9); // 默认长度9
			if (research.getPageNo() <= 0)
				research.setPageNo(1); // 默认页数1
			research.setiDisplayStart((research.getPageNo() - 1)
					* research.getiDisplayLength()); // 显示起始索引
			research.setStatus("1");// 状态 0：无效，1：有效
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			if (research.getKeyword() != null && !research.getKeyword().trim().isEmpty()) {
				resp.put("dataList",researchService.queryByLike(research, research.getKeyword()));
				resp.put("total",researchService.queryByLikeCount(research, research.getKeyword()));
			} else {
				List<Research> rs = researchService.query(research);
				resp.put("dataList", rs);
				resp.put("total",researchService.count(research));
				int lens = rs.size();
				List<Integer> numbersGAJ = new ArrayList<Integer>();
				List<Integer> numsGAJ = new ArrayList<Integer>();
				for(int i=0;i<lens;i++){
					Research temp = rs.get(i);
					String status = temp.getSignStatus();
					int cnt=0;
					if(status!=null && status!=""){
						cnt=status.split(",").length;//gaj signed
					}
					numbersGAJ.add(i, cnt);//公安局已签收数目
//					String[] tempAs = temp.getAssociations();
					String[] tempAs = temp.getDepars().split(",");
					int nums=0;
					if(tempAs!=null){
						for(int j=0;j<tempAs.length;j++){
							if(tempAs[j] != null && !(tempAs[j].equals(";")) && !(tempAs[j].equals(""))){
								nums++;
							}
						}
					}
					numsGAJ.add(i, nums);//公安局需要签收的单位数目
				}
				resp.put("numbersGAJ",numbersGAJ);
				resp.put("numsGAJ",numsGAJ);
			}
			resp.put("pageNo",research.getPageNo());
			resp.put("size",research.getiDisplayLength());
			resp.put("research",research);
			resp.put("key",research.getKeyword());
			resp.put("weekdays",weekdays);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询通知通报异常", e);
		}
		return resp;
	}
	
	
	
	//useless flowings.
	/**
	 * 查询列表
	 * @param Research
	 * @return
	 */
	@RequestMapping("/myResearch")
	public ModelAndView myResearch(Research research) {
		ModelAndView mv = new ModelAndView("/my/myResearchInfo.jsp");
		try {
			Member mbo = (Member) session.getAttribute("user");
			if (mbo == null) {
				mv.setViewName("404.jsp");
			} else {
				research.setUserid(mbo.getId());
				// 设置分页信息
				if (research.getiDisplayLength() == 0)
					research.setiDisplayLength(10); // 默认长度5
				if (research.getPageNo() <= 0)
					research.setPageNo(1); // 默认页数1
				research.setiDisplayStart((research.getPageNo() - 1)
						* research.getiDisplayLength()); // 显示起始索引
				mv.addObject("dataList", researchService.query(research));
				mv.addObject("pageNo", research.getPageNo());
				mv.addObject("size", research.getiDisplayLength());
				mv.addObject("total", researchService.count(research));
				mv.addObject("research", research);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return mv;
	}

	/**
	 * 删除新闻
	 * @param Research
	 * @return
	 */
	@RequestMapping("/delResearch")
	public ModelAndView delResearch(Research research, Long id) {
		ModelAndView mv = new ModelAndView("redirect:/research/myResearch");
		try {
			Member mbo = (Member) session.getAttribute("user");
			if (mbo == null) {
				mv.setViewName("404.jsp");
			} else {
				researchService.delete(id);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return mv;
	}

	/**
	 * 初始化新闻
	 * @param Research
	 * @return
	 */
	@RequestMapping("/initResearch")
	public ModelAndView initResearch(Research research, Long id) {
		ModelAndView mv = new ModelAndView("/my/researchDetail.jsp");
		mv.addObject("column",
				this.confService.queryByType(null/*GlobalConstant.TYPE_Research*/, 9667L));
		Member mbo = (Member) session.getAttribute("user");
		if (mbo == null) {
			mv.setViewName("404.jsp");
		} else {
			research = researchService.find(id);
			mv.addObject("data", research);
		}
		return mv;
	}

	/**
	 * 保存新闻
	 * @param Research
	 * @return
	 */
	@RequestMapping("/saveResearch")
	public ModelAndView saveResearch(Research research) {
		ModelAndView mv = new ModelAndView("redirect:/research/myResearch");
		Member mbo = (Member) session.getAttribute("user");
		if (mbo == null) {
			mv.setViewName("404.jsp");
		} else {
			research.setUserid(mbo.getId());
			research.setCreateTime(DateUtil.getCurrentDate2());
			researchService.saveOrUpdate(research);
		}
		return mv;
	}

}
